<!--
 * @Author: wangming
 * @Date: 2020-09-16 09:16:01
 * @LastEditors: wangming
 * @LastEditTime: 2021-10-22 11:13:30
 * @Description: file content
-->
<template>
  <date-picker
    v-if="!viewInfo"
    :disabled="isEdit || disabled"
    :style="{ width: config.__config.width + '%', height: '32px' }"
    :minute-step="config.__config.timefilter || 1"
    prefix-class="xmx"
    popupClass="reset-date-picker-zindex"
    :value="value"
    :type="dateConfig.type"
    valueType="timestamp"
    @change="changeDate"
    :format="config.__config.format"
    :editable="false"
    :disabled-date="disabledBeforeTodayAndAfterAWeek"
    :placeholder="config.__config.placeholder"
  >
  </date-picker>
  <div
    v-else
    :style="{
      display: 'inline-block',
      whiteSpace: 'break-spaces',
      width: config.__config.width + '%',
    }"
  >
    {{ time }}
  </div>
</template>

<script>
import myMixin from "@/pageComponents/myMixin";
import vmMixin from "@/pageComponents/vmMixin";
import { dateLimitTypeList, dateConfig } from "@/utils/dateLimitTypeList";
var moment = require("moment");
export default {
  computed: {
    time() {
      if (this.value) {
        return moment(this.value).format(this.config.__config.format);
      } else {
        return "";
      }
    },
    dateConfig() {
      return (
        dateConfig.find((i) => this.config.__config.format === i.format) || {}
      );
    },
    dateLimit() {
      return (
        dateLimitTypeList.find(
          (i) => i.value === this.config.__config.dateLimitType
        ) || {}
      );
    },
  },
  mixins: [myMixin, vmMixin],
  components: {},
  data() {
    return {};
  },
  async mounted() {
    if (!this.value) {
      if (this.config.__config.isCurrentDate && !this.form?.id) {
        this.$emit(
          "input",
          moment(moment().format(this.config.__config.format)).valueOf()
        );
      }
    }
    await this.$nextTick();
  },
  methods: {
    disabledBeforeTodayAndAfterAWeek(date) {
      if (!this.dateLimit.maxDate && !this.dateLimit.minDate) {
        return false;
      }
      if (!!this.dateLimit.maxDate && !!this.dateLimit.minDate) {
        return (
          date > new Date(this.dateLimit.maxDate) ||
          date < new Date(this.dateLimit.minDate)
        );
      }
      if (this.dateLimit.maxDate) {
        return date > new Date(this.dateLimit.maxDate);
      }
      if (this.dateLimit.minDate) {
        return date < new Date(this.dateLimit.minDate);
      }
    },
    changeDate(date) {
      this.$emit("input", date || null);
      this.runEventName("change", { vlaues: date || null });
    },
  },
};
</script>


